// index.js
// 获取应用实例
const app = getApp()
const QQMapWX = require('../qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js');
const carts = require('../../utils/cart')
Page({
  data: {
    // 轮播图地址数组
    images: ['../image/8.webp', '../image/7.webp', '../image/6.webp', '../image/5.webp', '../image/4.webp'],
    location: '',
    meunHight: 0,
    shops: [{
      category: '强烈推荐',
      goods: [
        {
          _id: 1,
          img: ['/pages/image/goods1.jpg'],
          goods_name: '芝士草莓',
          goods_desc: 'sklacmklamlkcmla',
          goods_price: 35,
          goods_count: 100
        },
        {
          _id: 2,
          img: ['/pages/image/goods2.jpg'],
          goods_name: '芝士苹果',
          goods_desc: 'sklacmklamlkcmla',
          goods_price: 35,
          goods_count: 0
        },
      ]
    },
    {
      category: '当季限定',
      goods: [
        {
          _id: 3,
          img: ['/pages/image/goods1.jpg'],
          goods_name: '芝士草莓',
          goods_desc: 'sklacmklamlkcmla',
          goods_price: 35,
          goods_count: 10
        },
        {
          _id: 4,
          img: ['/pages/image/goods2.jpg'],
          goods_name: '芝士苹果',
          goods_desc: 'sklacmklamlkcmla',
          goods_price: 35,
          goods_count: 10
        },
      ]
    },
    {
      category: '霸气鲜果',
      goods: [
        {
          _id: 1,
          img: ['/pages/image/goods1.jpg'],
          goods_name: '芝士草莓',
          goods_desc: 'sklacmklamlkcmla',
          goods_price: 35,
          goods_count: 10
        },
        {
          _id: 1,
          img: ['/pages/image/goods2.jpg'],
          goods_name: '芝士苹果',
          goods_desc: 'sklacmklamlkcmla',
          goods_price: 35,
          goods_count: 10
        },
      ]
    }
    ],
    selectedIndex: 0,
    selectCategory: '当季限定',
    tops: [],
    shopListTop: 0,
    pop_visible: false,
    pop_detail: {},
    defaultNum:0
  },
  //count组件上的加入购物车
  addcart(e){
    let {detail}=e.currentTarget.dataset
    // console.log(detail)
    // const {num}
    // if(detail.goods_count>detail.num){
      // console.log(1);
      detail.num=1
      carts.add(detail)
      console.log(wx.getStorageSync('carts'));
      
    // }else{
    //   wx.showToast({
    //     title: '商品不足',
    //     icon:'error'
    //   })
    // }
  },
  minuscart(e){
    let {detail}=e.currentTarget.dataset
    detail.num=-1
    carts.add(detail)
  },
  //弹窗加入购物车
  cart(e){
    const {detail}=e.detail
    console.log(detail)
    carts.add(detail)
    //隐藏商品弹窗
    this.setData({
      pop_visible:false
    })
  },
  //弹出弹窗
  pop(e) {
    const { count } = e.currentTarget.dataset
    const { price } = e.currentTarget.dataset
    const { name } = e.currentTarget.dataset
    const { img } = e.currentTarget.dataset
    if (count < 1) {
      wx.showToast({
        title: '该商品已售罄',
        icon: 'error'
      })
      return
    }
    this.setData({
      pop_detail: {
        img: [img], goods_name: name,
        goods_price: price,
        goods_count: count
      },
      pop_visible: true
    })
  },
  //商品列表滚动事件
  shopListScroll(e) {
    const { scrollTop } = e.detail
    const { tops } = this.data
    for (let i = 0; i < tops.length; i++) {
      if (scrollTop > tops[i] && scrollTop < tops[i + 1]) {
        this.setData({
          selectCategory: this.data.shops[i].category,
          selectedIndex: i
        })
      }
    }
  },
  //获取滚动条高度
  getTops() {
    return new Promise((resolve, reject) => {
      wx.createSelectorQuery().selectAll('.shop_list_item').boundingClientRect(e => {
        let tops = []
        tops.push(0)
        e.forEach((v, i) => {
          tops.push(tops[i] + v.height)
        })
        // console.log(tops)
        this.setData({
          tops: tops
        })
        resolve('success')
      }).exec()
    })

  },
  // 选择商品分类
  selectedCategory(e) {
    console.log(e)
    const { index } = e.currentTarget.dataset
    const { tops } = this.data
    this.setData({
      selectedIndex: index,
      selectCategory: this.data.shops[index].category,
      shopListTop: tops[index]
    })
  },
  // 设置商品列表高度（自适应屏幕大小）
  setHeight() {
    const screenHeigth = wx.getSystemInfoSync().windowHeight
    const dpr = wx.getSystemInfoSync().pixelRatio
    wx.createSelectorQuery().select('.swiper').boundingClientRect(e => {
      console.log(e)
      this.setData({
        meunHight: screenHeigth - e.height - 60 / dpr
      })
    }).exec()
    console.log(screenHeigth)
  },
  // 获取地址信息
  myLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: res => {
        console.log(res)
        const qqmap = new QQMapWX({
          key: 'XA4BZ-UXSLD-YHZ4U-P4XTC-Y4OXJ-WZBEH'
        })
        qqmap.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: res => {
            console.log(res)
            this.setData({
              location: res.result.address
            })
          }
        })
      }
    })
  },
  onLoad() {
    this.myLocation()
    this.setHeight()
  },
  async onReady() {
    await this.getTops().then(res => {
      console.log(res)
      console.log(this.data.tops)
    })
  },
  onShow() {
    carts.setBadge()
  },
  onHide(){
    this.setData({
      defaultNum:0
    })
  }
})
